<template>
  <Dialog title="语音文件管理" v-model="dialogVisiblelist" width="1000">
    <div class="call-dialog">
      <div style="width: 100%; display: flex; justify-content: flex-end">
        <el-button type="primary" @click="openForm('create')">
          <Icon icon="ep:plus" class="mr-5px" />
          新增
        </el-button>
      </div>
      <el-table :data="list" style="width: 100%">
        <el-table-column prop="name" label="文件名称" align="center"> </el-table-column>
        <el-table-column prop="remark" label="备注" align="center"> </el-table-column>
        <el-table-column prop="path" label="文件路径" align="center" show-overflow-tooltip>
        </el-table-column>
        <el-table-column
          prop="createTime"
          :formatter="dateFormatter"
          label="创建时间"
          align="center"
        >
        </el-table-column>
        <el-table-column prop="prop" label="操作" align="center">
          <template #default="scope">
            <el-button type="primary" @click="handlePlay(scope.row.path)" link>播放</el-button>
            <el-button type="success" @click="handleDown(scope.row.path)" link>下载</el-button>
            <el-button type="danger" @click="handleDelete(scope.row.id)" link>删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
  </Dialog>
  <VoiceForm ref="callFormRef" @success="getList" />
  <!-- 播放音频 -->
  <PlayVoice ref="playVoiceRef" />
</template>

<script setup>
import VoiceForm from './voiceForm.vue'
import { dateFormatter } from '@/utils/formatTime'
import * as taskApi from '@/api/call/task'
import PlayVoice from './playVoice.vue'

const message = useMessage()
const { t } = useI18n()
const dialogVisiblelist = ref(false)
const callFormRef = ref(null)
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10
})
const total = ref(0)
const list = ref([])
const open = () => {
  dialogVisiblelist.value = true
  queryParams.pageNo = 1
  getList()
}
// 列表
const getList = async () => {
  const data = await taskApi.getVoiceFilePage(queryParams)
  list.value = data.list
  total.value = data.total
}
defineExpose({ open })

/*新增呼叫名单*/
const openForm = (type, id) => {
  callFormRef.value.open(type, id)
}
/*删除*/
const handleDelete = async (id) => {
  try {
    await message.delConfirm()
    await taskApi.deleteVoiceFile(id)
    ElMessage.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}
const playVoiceRef = ref(null)
/*播放*/
const handlePlay = async (url) => {
  playVoiceRef.value.open(url)
}
/*下载*/
const handleDown = (url) => {
  if (url) {
    const link = document.createElement('a')
    link.href = url
    link.setAttribute('download', 'recording.mp4')
    document.body.appendChild(link)
    link.click()
    document.body.removeChild(link)
  }
}
</script>

<style lang="scss" scoped>
.call-dialog {
  padding-bottom: 90px !important;
}
</style>
